using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace MyLib.Utils
{
    public class SerializationUtil
    {
        public static string XMLSerialize(object obj)
        {
            XmlSerializer ser = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            ser.Serialize(stream, obj);
            stream.Position = 0;
            return StringUtil.GetString(stream);
        }

        public static T XMLDeserialize<T>(string xmlString) where T : class
        {
            Stream value = StringUtil.GetStream(xmlString);
            object obj = XMLDeserialize(value, typeof(T));
            return (T)obj;
        }

        public static Stream XMLSerializeForStream(object value)
        {
            XmlSerializer ser = new XmlSerializer(value.GetType());
            MemoryStream stream = new MemoryStream();
            ser.Serialize(stream, value);
            stream.Position = 0;
            return stream;
        }

        public static object XMLDeserialize(Stream value, Type type)
        {
            XmlSerializer ser = new XmlSerializer(type);
            return ser.Deserialize(value);
        }

        public static T XMLDeserialize<T>(Stream value) where T : class
        {
            object obj = XMLDeserialize(value, typeof(T));
            return (T)obj;
        }

        public static Stream StreamSerialize(object value)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            bf.Serialize(stream, value);
            stream.Position = 0;
            return stream;
        }

        public static object StreamDeserialize(Stream value)
        {
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(value);
        }

        public static T StreamDeserialize<T>(Stream value) where T : class
        {
            BinaryFormatter bf = new BinaryFormatter();
            return (T)bf.Deserialize(value);
        }

        public static void FileSerialize(string fileName, object value)
        {
            FileStream fs = null;
            try
            {
                XmlSerializer ser = new XmlSerializer(value.GetType());
                fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                ser.Serialize(fs, value);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        public static object FileDeserialize(string fileName, Type type)
        {
            FileStream fs = null;
            try
            {
                XmlSerializer ser = new XmlSerializer(type);
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                return ser.Deserialize(fs);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        public static T FileDeserialize<T>(string fileName) where T : class
        {
            object obj = FileDeserialize(fileName, typeof(T));
            return (T)obj;
        }
    }
}
